package GUI;

import java.awt.Component;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JProgressBar;
import javax.swing.JTable;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumnModel;

public class TableFrame extends javax.swing.JFrame {

    /** Creates new form TableFrame */
    public TableFrame() {
        initComponents();
        
        // I added this code to NB generated code
        
        final List<JProgressBar> bars = new ArrayList<JProgressBar>(); 
        final AbstractTableModel model = ((AbstractTableModel) table.getModel());
        for (int i = 0; i < table.getRowCount(); i++) {
            bars.add(new JProgressBar(0, 100));
        }
        TableColumnModel columnModel = table.getColumnModel();
       
        columnModel.getColumn(2).setCellRenderer(new TableCellRenderer() { //sets a progress bar as renderer
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                JProgressBar bar = bars.get(row);
                bar.setValue((Integer) table.getValueAt(row, 1));
                return bar;
            }
        });
        
        model.addTableModelListener(new TableModelListener(){
            public void tableChanged(TableModelEvent e) {
                int c = e.getColumn();
                int r = e.getFirstRow();
                if(c==1){
                    model.fireTableChanged(new TableModelEvent(model,r));  //update the whole row
                }
            }
        });

        //end of my own code
    
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">
    private void initComponents() {

        scrollPane = new javax.swing.JScrollPane();
        table = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"Task 1", new Integer(60), null, new Integer(5)},
                {"Task 2", new Integer(23), null, new Integer(5)}
            },
            new String [] {
                "Name", "Percent", "Progress", "Another int"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Object.class, java.lang.Integer.class, java.lang.Object.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                true, true, false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        scrollPane.setViewportView(table);

        getContentPane().add(scrollPane, java.awt.BorderLayout.PAGE_START);

        pack();
    }// </editor-fold>
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new TableFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify
    private javax.swing.JScrollPane scrollPane;
    private javax.swing.JTable table;
    // End of variables declaration
}